﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfBinding_Path
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<string> stringList = new List<string>() { "Tim", "Tom", "Blog" };
            this.listTextBox1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList });
            this.listTextBox2.SetBinding(TextBox.TextProperty,
                new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay });
            this.listTextBox3.SetBinding(TextBox.TextProperty,
                new Binding("/[2]") { Source = stringList, Mode = BindingMode.OneWay });

            List<Country> countryList = new List<Country>();
            // 初始化countryList
            List<City> cityList = new List<City>();
            cityList.Add(new City() { Name = "成都" });
            List<Province> provinceList = new List<Province>();
            provinceList.Add(new Province() { Name = "四川", CityList = cityList });
            Country country = new Country() { Name = "中国", ProvinceList = provinceList };
            countryList.Add(country);

            // 绑定
            this.listInnerTextBox1.SetBinding(TextBox.TextProperty, 
                new Binding("/Name") { Source = countryList });
            this.listInnerTextBox2.SetBinding(TextBox.TextProperty, 
                new Binding("/ProvinceList[0].Name") { Source = countryList });
            this.listInnerTextBox3.SetBinding(TextBox.TextProperty, 
                new Binding("/ProvinceList/CityList[0].Name") { Source = countryList });
        }
    }

    class City
    {
        public string Name { get; set; }
    }

    class Province
    {
        public string Name { get; set; }
        public List<City> CityList { get; set; }
    }

    class Country
    {
        public string Name { get; set; }

        public List<Province> ProvinceList { get; set; }
    }
}
